# Copyright (c) 2007 Fabio Makoto Akita
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Author: AkitaOnRails (www.akitaonrails.com)
# Support: Surgeworks LLC (www.surgeworks.com)

#
# WARNING: DO NOT EDIT THIS FILE MANUALLY - IT IS AUTOMATICALLY GENERATED BY THE PLUGIN
#
module SyncsHelper
  # All communication is held through HTTP encapsulation. This support relies
  # on Ruby's own Net:HTTP library. This is just a helper to make it easier to
  # make such connections
  module NetSupport
    private
  
    # GET helper for http_open
    def http_open_get(address, uri, &block)
      http_open 'get', address, uri, &block
    end

    # POST helper for http_open
    def http_open_post(address, uri, &block)
      http_open 'post', address, uri, &block
    end

    # Encapsulates Proxy authentication, Basic HTTP authentication, 
    # Cookie handling, and SSL all at the same time 
    def http_open(method, address, uri, &block)
      sURI = URI.parse(address) 
      # deal if HTTP proxies
      http_conn = unless $SYNC_CONFIG[:proxy_host].blank?
        Net::HTTP.new(sURI.host, sURI.port, $SYNC_CONFIG[:proxy_host], $SYNC_CONFIG[:proxy_port], $SYNC_CONFIG[:proxy_user], $SYNC_CONFIG[:proxy_pass])
      else
        Net::HTTP.new(sURI.host, sURI.port)
      end
      # deal if SSL
      if $SYNC_CONFIG[:protocol] == 'https'
        http_conn.use_ssl = true 
        http_conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      # deal with cookie trafic
      url_header = nil
      if session['server-cookie']
        url_header = {'Cookie' => session['server-cookie']} 
        logger.info "SENDING COOKIE: #{url_header.to_yaml}"
      end
      req = http_conn.start do |http|
        if method == 'get'
          request = Net::HTTP::Get.new(sURI.path + uri, url_header)
        else
          request = Net::HTTP::Post.new(sURI.path + uri, url_header) 
        end
        # deal with HTTP Basic Authentication
        request.basic_auth($SYNC_CONFIG[:auth_user], $SYNC_CONFIG[:auth_pass]) unless $SYNC_CONFIG[:auth_user].blank?
    
        block.call(request) if block
        logger.info "Request Info: #{request.to_yaml}"
        http.request(request)
      end
      if req.response['set-cookie'] && session['server-cookie'].nil?
        session['server-cookie'] = req.response['set-cookie'].split('; ')[0]
      end
      logger.info "Requested: #{method} #{address}#{uri}"
      req
    end
  
    def expand(content = "")
      Zlib::Inflate.inflate(content)
    end
  
    def compress(content = "")
      Zlib::Deflate.deflate(content,9)
    end
  end
end